package com.example.wm.Secondary.Contoller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.wm.Secondary.Mapper.GoodsOrdersMapper;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;
import com.example.wm.Secondary.Entity.GoodesOrders;
import com.example.wm.Secondary.Entity.GoodsOrderNotes;
import com.example.wm.Secondary.Service.GoodsOrderNotesService;
import com.example.wm.Secondary.Service.GoodsOrdersService;
import com.example.wm.Secondary.Vo.GoodsOrdersVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

//@Slf4j
@RestController
@RequestMapping("/GoodsOrderNotes")
public class GoodsOrderNotesController {
    @Resource
    private GoodsOrderNotesService goodsOrderNotesService;
    @Resource
    private GoodsOrdersService goodsOrdersService;
    @Resource
    private GoodsOrdersMapper goodsOrdersMapper;
//    @Transactional(transactionManager = "secondaryTransactionManager")
    @PostMapping("/search")
    public Result search(@RequestBody GoodesOrders goodesOrders) {
        LambdaQueryWrapper<GoodesOrders> lq=new LambdaQueryWrapper<>();
        lq.like(GoodesOrders::getName,goodesOrders.getName());
        List<GoodesOrders> lists=goodsOrdersMapper.selectList(lq);
        GoodsOrdersVo goodsOrdersVo=new GoodsOrdersVo();
        ArrayList<GoodsOrdersVo>  arrayList=new ArrayList<>();
        if(!lists.isEmpty()){
        for (GoodesOrders g:lists){
            LambdaQueryWrapper<GoodsOrderNotes> lq2=new LambdaQueryWrapper<>();
            lq2.eq(GoodsOrderNotes::getG_id,g.getId());
            List<GoodsOrderNotes> list=goodsOrderNotesService.list(lq2);
            int n=0;//已经到吨数
            int n2=0;//剩余吨数
            if(!list.isEmpty()){
                for (GoodsOrderNotes goo:list){
                    n+= Integer.parseInt(goo.getNote_text());
                }
                n2= Integer.parseInt(g.getTonnage())-n;
            }
            goodsOrdersVo.setName(g.getName());
            goodsOrdersVo.setDoneton(n);
            goodsOrdersVo.setPrice(g.getPrice());
            goodsOrdersVo.setCreated_at(String.valueOf(g.getCreated_at()));
            goodsOrdersVo.setLeftton(n2);
            goodsOrdersVo.setTonnage(Integer.valueOf(g.getTonnage()));
            arrayList.add(goodsOrdersVo);
        }
        }

         return ResultData.success(arrayList);
    }


}
